#include "mainwindow.h"

Mainwindow::Mainwindow(QWidget *parent) : QWidget(parent)
{
    this->initView();
    this->setWindowTitle("Cn Space Style");
    this->resize(MAX_WINDOW_W, MAX_WINDOW_H);
}

Mainwindow::~Mainwindow() {}

void Mainwindow::initView()
{
    struct iconDef navItems[] = {{QIcon(":resource/home-filling.png"), QString("首页")},
                                 {
                                     QIcon(":resource/setting-filling.png"),
                                     QString("设置"),
                                 },
                                 {
                                     QIcon(":resource/notification-filling.png"),
                                     QString("告警"),
                                 },
                                 {
                                     QIcon(":resource/comment-filling.png"),
                                     QString("通信"),
                                 },
                                 {
                                     QIcon(":resource/map-filling.png"),
                                     QString("定位"),
                                 },
                                 {
                                     QIcon(":resource/warning-filling.png"),
                                     QString("故障"),
                                 }};

    /// 0、创建主要的界面元素
    this->m_StackedWidget = new QStackedWidget(this);

    /// 1、先新建Navbar的按钮
    for (uint i = 0; i < sizeof(navItems) / sizeof(navItems[0]); i++)
    {
        QToolButton *btn = new QToolButton(this);
        btn->setProperty("nav_index", i);
        btn->setIconSize(QSize(48, 48));
        btn->setIcon(navItems[i].icon);
        btn->setText(navItems[i].title);
        btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        btn->setMinimumWidth(96);
        btn->setMinimumHeight(134);
        btn->setCheckable(true);

        connect(btn, SIGNAL(clicked(bool)), this, SLOT(onNabitemClicked(bool)));

        this->m_NavItems.append(btn);

        /// 简单的添加一下示例界面
        //        this->m_StackedWidget->addWidget( new DataDispWidget( navItems[ i ].title, "", this ) );
        //        GaugePanel*   gp = new GaugePanel( this );
        //        SummaryPanel* sp = new SummaryPanel( 8, this );
        //        m_HomePage = new HomePage( this );

        //        gp->setFixedSize( 128, 128 );
        //        gp->setValue( 10 );
        //        this->m_StackedWidget->addWidget( m_HomePage );
    }

    m_HomePage = new HomePage(this);
    this->m_StackedWidget->addWidget(m_HomePage);

    this->m_NavItems.at(0)->setChecked(true);

    /// 2、Layout界面
    QVBoxLayout *btnLayout = new QVBoxLayout;
    btnLayout->setContentsMargins(0, 0, 0, 0);
    btnLayout->setSpacing(0);
    for (int i = 0; i < this->m_NavItems.length(); i++)
    {
        btnLayout->addWidget(m_NavItems.at(i));

        if (i == this->m_NavItems.length() - 2)
        {
            btnLayout->addStretch(1);
        }
    }

    QHBoxLayout *layout = new QHBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addLayout(btnLayout);
    layout->addWidget(this->m_StackedWidget);

    /// 3、配置下Layout刷新下背景色
    this->setToolButtonStyle();
    this->setAutoFillBackground(true);
    QPalette palette;
    palette.setBrush(this->backgroundRole(), QColor("#504f4b"));
    this->setPalette(palette);
    this->setLayout(layout);
}

void Mainwindow::setToolButtonStyle()
{
    QString style = "QToolButton{"
                    "border-style:none;"
                    "border:0px;"
                    "color:#ffffff;"
                    "font-size:18px;"
                    "background:#36353a;"
                    "padding-top:18px;"
                    "}"
                    "QToolButton:hover{"
                    "background:#2fb4fe;"
                    "}"
                    "QToolButton:checked{"
                    "background:#2fb4fe;"
                    "}"
                    "QToolButton:pressed{"
                    "background:#2fb4fe;"
                    "}";

    for (int i = 0; i < this->m_NavItems.length(); i++)
    {
        this->m_NavItems.at(i)->setStyleSheet(style);
    }
}

void Mainwindow::onNabitemClicked(bool clicked)
{
    Q_UNUSED(clicked)
    QToolButton *btn = dynamic_cast<QToolButton *>(sender());

    if (btn != Q_NULLPTR)
    {
        foreach (QToolButton *b, this->m_NavItems)
        {
            b->setChecked(false);
        }

        btn->setChecked(true);
        this->m_StackedWidget->setCurrentIndex(btn->property("nav_index").toInt());

        qDebug() << "click " << btn->property("nav_index").toInt();
    }
}
